package com.bskj.kuaifu.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.bskj.common.bean.model.UserInvoice;
import com.bskj.common.enums.UserInvoiceStatusEnum;
import com.bskj.common.utils.EncryptUtil;
import com.bskj.common.web.BaseController;
import com.bskj.kuaifu.bean.vo.resp.UserInvoiceVo;
import com.bskj.kuaifu.service.IUserInvoiceService;

@Controller
@RequestMapping("invoice")
public class UserInvoiceController extends BaseController {

	@Autowired
	private IUserInvoiceService userInvoiceService;
	
	@RequestMapping("index")
	public ModelAndView toIndex(Integer userId) throws Exception {
		ModelAndView mv=new ModelAndView();
		UserInvoiceVo userInvoiceVo=userInvoiceService.FindInvMoney(userId);
		mv.addObject("userInvoiceVo", userInvoiceVo);
		mv.setViewName("/invoice/index");
		
		JSONObject invoiceParam = new JSONObject();
		invoiceParam.put("uri", "/invoice/toApply");
		invoiceParam.put("userId", userId);
		invoiceParam.put("money", userInvoiceVo.getMoney());
        mv.addObject("invoiceParam", EncryptUtil.encrypt(invoiceParam.toJSONString()));
        
		return mv;
	}
		
	@RequestMapping("toApply")
	public ModelAndView toApply(Integer userId,Integer money) throws Exception {
		ModelAndView mv=new ModelAndView();
		mv.setViewName("/invoice/apply");
		
		JSONObject success = new JSONObject();
	    success.put("uri", "/invoice/success");
	    success.put("userId", userId);
	    mv.addObject("successParams", EncryptUtil.encrypt(success.toJSONString()));
		
		JSONObject applyParam = new JSONObject();
		applyParam.put("uri", "/invoice/apply");
		applyParam.put("userId", userId);
		applyParam.put("money", money);
        mv.addObject("applyParam", EncryptUtil.encrypt(applyParam.toJSONString()));
        
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("apply")
	public Map<String, Object> apply(UserInvoice userInvoice) {
		Map<String, Object> resultMap=new HashMap<>();
		userInvoice.setStatus(UserInvoiceStatusEnum.complaint.getKey());
		userInvoice.setCreateTime(new Date());
		userInvoice.setUpdateTime(new Date());
		userInvoiceService.insert(userInvoice);
		resultMap.put("result", true);
		return resultMap;
	}
	
	 @RequestMapping("/success")
	    public ModelAndView toSuccess() {
	    	ModelAndView mv=new ModelAndView();
	    	mv.setViewName("/invoice/ok");
	    	return mv;
		}
}
